package com.abigdreamer.im.client.module.login;

import java.lang.reflect.InvocationTargetException;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.abigdreamer.im.client.module.main.EIMMainFrame;
import com.abigdreamer.im.client.module.messageProcess.IMessageProcess;
import com.abigdreamer.im.client.module.network.socket.ClientChatSocket;
import com.abigdreamer.im.client.module.network.socket.ConnectSession;
import com.abigdreamer.im.client.ui.BaseFrame;
import com.abigdreamer.im.client.ui.trayIcon.EIMTrayIcon;
import com.abigdreamer.im.client.util.EIMClientConfig;
import com.abigdreamer.im.message.BaseMessage;
import com.abigdreamer.im.message.RepeatLoginMessage;

public class RepeatLoginMessageProcess implements IMessageProcess {

	public void processMessage(BaseMessage message) {
		RepeatLoginMessage repeatMessage = (RepeatLoginMessage) message;
		final String ip = repeatMessage.getRepeatLoginIP();
		Runnable run = new Runnable() {
			EIMMainFrame mainFrame = EIMMainFrame.getInstance();

			public void run() {
				EIMTrayIcon qqTrayIcon = EIMTrayIcon.getInStance();
				qqTrayIcon.showIcon(EIMClientConfig.OffLineTryIcon_Type);
				ClientChatSocket.getInstance().setStart(false);
				ConnectSession.getInstance().close();
				String errorInfo = "你的QQ在Ip为:" + ip + "的地方登陆了";
				Object[] options = { "重新登录", "退出", "取消" };
				BaseFrame.centerWindow(mainFrame);
				mainFrame.setVisible(true);
				int response = JOptionPane.showOptionDialog(mainFrame, errorInfo, "重新登录", JOptionPane.YES_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
				if (response == 0) {
					mainFrame.setVisible(false);
					EIMLoginWindow qqLoginFrame = EIMLoginWindow.getInstance();
					qqLoginFrame.setVisible(true);
				} else if (response == 1) {
					System.exit(0);
				} else if (response == 2) {

				}
			}
		};
		try {
			SwingUtilities.invokeAndWait(run);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

	}

}
